layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    const $previewMask = $('<div></div>').css({
        position: 'fixed',
        top: 0, left: 0, right: 0, bottom: 0,
        backgroundColor: 'rgba(0,0,0,0.5)',
        display: 'none',
        justifyContent: 'center',
        alignItems: 'center',
        zIndex: 9999
    }).appendTo('body');

    const $previewVideo = $('<video controls autoplay></video>').css({
        maxWidth: '90%',
        maxHeight: '90%',
        boxShadow: '0 0 20px rgba(0,0,0,0.5)',
        borderRadius: '8px',
        display: 'none',
    }).appendTo($previewMask);

    const $previewImg = $('<img>').css({
        maxWidth: '90%',
        maxHeight: '90%',
        boxShadow: '0 0 20px rgba(0,0,0,0.5)',
        display: 'none',
        borderRadius: '8px'
    }).appendTo($previewMask);

    // 点击遮罩关闭
    $previewMask.on('click', function () {
        $previewImg.hide();
        $previewVideo.hide();
        $previewVideo[0].pause();
        $previewMask.hide();
    });

    // 事件委托绑定点击事件
    $(document).on('click', '.layui-upload-img-expand', function () {
        const imgSrc = $(this).attr('src');
        if (imgSrc) {
            $previewImg.show();
            $previewImg.attr('src', imgSrc);
            $previewMask.css('display', 'flex');
        }
    });

    $(document).on('click', '.layui-upload-video-expand', function () {
        const imgSrc = $(this).attr('src');
        if (imgSrc) {
            $previewVideo.show();
            $previewVideo.attr('src', imgSrc);
            $previewMask.css('display', 'flex');
        }
    });

    var enumAjax = new $ax(Feng.ctxPath + "/enums/checkStatus");
    var enumRes = enumAjax.start();
    var projects = enumRes.data;
    var html = "";
    for(var key in projects){
        html += '<option value="'+key+'">'+projects[key]+'</option>'
    }
    $("#checkStatus").append(html);
    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/nanSellAnnounce/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('nanSellAnnounceForm', result.data);
    var h = '<div class="layui-upload-wrap">'
    result.data.thumb.split(',').forEach(function (item) {
        h += '<img class="layui-upload-img layui-upload-img-expand" src="'+Feng.cdn+ item + '" >'
    })
    h += '</div>'
    $("#images").html(h);
    if(result.data.video){
        $('#videoContainer').css('display','block');
        $('#video').attr('src', Feng.cdn + result.data.video);
    }
    $("#content").html(result.data.content);
    form.render('select');

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/nanSellAnnounce/editItem", function (data) {
            if(data.success){
                Feng.success("操作成功！");
                        //传给上个页面，刷新table用
                        admin.putTempData('formOk', true);
                        //关掉对话框
                        admin.closeThisDialog();
            }else{
                Feng.error("操作失败！" + data.message)
            }
        }, function (data) {
            Feng.error("操作失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
        return false;
    });

});
